<?php
require_once ("cfginicial.php");
require_once(INCLUDE_DIR."/DBSetup.inc.php");
require_once (INCLUDE_DIR."/funciones_generales.php");
require_once (INCLUDE_DIR."/funciones_menues.php");
require_once (INCLUDE_DIR."/funciones_sesion.php");
require_once (INCLUDE_DIR."/funciones_fechas.php");

################################################################################   
## +---------------------------------------------------------------------------+
## | 1. Creating & Calling:                                                    | 
## +---------------------------------------------------------------------------+
##  *** only relative (virtual) path (to the current document)
  define ("DATAGRID_DIR", "datagrid_4.2.8/");
  define ("PEAR_DIR", "datagrid_4.2.8/pear/");
  
  require_once(DATAGRID_DIR.'datagrid.class.php');
  require_once(PEAR_DIR.'PEAR.php');
  require_once(PEAR_DIR.'DB.php');

##  *** creating variables that we need for database connection 
  $DB_USER=DBUSER;            
  $DB_PASS=DBPASSW;           
  $DB_HOST=SERVIDOR.':'.DBPORT;       
  $DB_NAME=DATABASE;    

ob_start();
  $db_conn = DB::factory('pgsql'); 
  $db_conn -> connect(DB::parseDSN('pgsql://'.$DB_USER.':'.$DB_PASS.'@'.$DB_HOST.'/'.$DB_NAME));


##  *** put a primary key on the first place 
  $sql=" SELECT "  
   ."t_tipo_usuario.id, "
   ."t_tipo_usuario.id_tipo_cia, "
   ."t_tipo_cia.descripcion as tipo_cia_desc, "
   ."t_tipo_usuario.descripcion, "
   ."t_tipo_usuario.nivel_autoridad, "
   ." CASE WHEN t_tipo_usuario.flag_habilitado = 'S' THEN 'Ok' ELSE 'No' END as habilitado "
//   ." CASE WHEN countries.is_democracy = 1 THEN 'Yes' ELSE 'No' END as is_democracy "
   ."FROM t_tipo_usuario "
	." INNER JOIN t_tipo_cia on t_tipo_usuario.id_tipo_cia = t_tipo_cia.id"	;
   
##  *** set needed options
  $debug_mode = false;
  $messaging = true;
  $unique_prefix = "f_";  

  $dgrid = new DataGrid($debug_mode, $messaging, $unique_prefix, DATAGRID_DIR);
##  *** set data source with needed options
  $default_order_field = "tipo_cia_desc";
  $default_order_type = "ASC";
  $dgrid->dataSource($db_conn, $sql, $default_order_field, $default_order_type);	    

	$dg_language = "es";  
	$dgrid->SetInterfaceLang($dg_language);

##  *** set modes for operations ("type" = "link|button|image"),
##  *** "view" - view mode | "edit" - add/edit/details modes
##  *** ("byFieldValue" - make the field as a link to edit mode page)

$modes = array(
  "add"     =>array("view"=>false, "edit"=>false, "type"=>"button","show_add_button"=>"outside"),
  "edit"    =>array("view"=>false,"edit"=>true, "type"=>"link",  "byFieldValue"=>""),
  "cancel"  =>array("view"=>true, "edit"=>true, "type"=>"link"),
  "details" =>array("view"=>true, "edit"=>false, "type"=>"link"),
  "delete"  =>array("view"=>false, "edit"=>true, "type"=>"image")
);
$dgrid->SetModes($modes);


## +---------------------------------------------------------------------------+
## | 3. Printing & Exporting Settings:                                         | 
## +---------------------------------------------------------------------------+
##  *** set printing option: true(default) or false 
 $printing_option = true;
 $dgrid->allowPrinting($printing_option);
##  *** set exporting option: true(default) or false 
 $exporting_option = false;
 $dgrid->allowExporting($exporting_option);

## +---------------------------------------------------------------------------+
## | 4. Sorting & Paging Settings:                                             | 
## +---------------------------------------------------------------------------+
##  *** set sorting option: true(default) or false 
 $sorting_option = false;
 $dgrid->allowSorting($sorting_option);               
##  *** set paging option: true(default) or false 
 $paging_option = true;
 $rows_numeration = false;
 $numeration_sign = "N #";       
 $dgrid->allowPaging($paging_option, $rows_numeration, $numeration_sign);
##  *** set paging settings
 $bottom_paging = array("results"=>true, "results_align"=>"left", "pages"=>true, "pages_align"=>"center", "page_size"=>true, "page_size_align"=>"right");
 $top_paging = array();
 $pages_array = array("10"=>"10", "25"=>"25", "50"=>"50");
 $default_page_size = 10;
 $dgrid->setPagingSettings($bottom_paging, $top_paging, $pages_array, $default_page_size);
##

## +---------------------------------------------------------------------------+
## | 5. Filter Settings:                                                       | 
## +---------------------------------------------------------------------------+
##  *** set filtering option: true or false(default)
 $filtering_option = false;
 $dgrid->allowFiltering($filtering_option);
##

## +---------------------------------------------------------------------------+
## | 6. View Mode Settings:                                                    | 
## +---------------------------------------------------------------------------+
##  *** set columns in view mode
//   $dgrid->setAutoColumnsInViewMode(true);  
 $vm_colimns = array(
	"tipo_cia_desc"  =>array("header"=>"Tipo de Cia",      
				"type"=>"label", 
				"width"=>"90px", 
				"align"=>"left",   
				"wrap"=>"nowrap", 
				"text_length"=>"-1", "case"=>"normal"),

	"descripcion"   =>array("header"=>"Descripcion",  
		"type"=>"label",  "align"=>"left",  "width"=>"50px",
		"wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),

	"nivel_autoridad"   =>array("header"=>"Nivel Autoridad",  
		"type"=>"label",  "align"=>"center",  "width"=>"50px",
		"wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),

	"habilitado" 	=>array("header"=>"Habilitado",     
				"type"=>"label", "align"=>"center", 
				"width"=>"130px", "wrap"=>"nowrap", "text_length"=>"-1", 
				"case"=>"normal",  "on_js_event"=>""),
    //"population"   =>array("header"=>"Population",       "type"=>"label", "summarize"=>true, "align"=>"right",  "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
    //"presidents"   =>array("header"=>"Presidents",       "type"=>"label", "summarize"=>true, "align"=>"right",  "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal"),
    //"description"  =>array("header"=>"Short Description","type"=>"label", "align"=>"left",   "wrap"=>"wrap",   "text_length"=>"15", "case"=>"lower"),
//    "picture_url"  =>array("header"=>"Picture",          "type"=>"image", "align"=>"center", "width"=>"", "wrap"=>"nowrap", "text_length"=>"-1", "case"=>"normal", "summarize"=>false, "on_js_event"=>"", "target_path"=>"uploads/", "default"=>"", "image_width"=>"17px", "image_height"=>"17px"),
  );
  $dgrid->setColumnsInViewMode($vm_colimns);


## +---------------------------------------------------------------------------+
## | 7. Add/Edit/Details Mode settings:                                        | 
## +---------------------------------------------------------------------------+
##  ***  set settings for edit/details mode
  $table_name = "t_tipo_usuario";
  $primary_key = "id";
  $condition = "";
  $dgrid->setTableEdit($table_name, $primary_key, $condition);

$em_columns = array(
    "id_tipo_cia"      =>array("header"=>"Tipo de Cia",      "type"=>"textbox",  "width"=>"210px", "req_type"=>"rt", "title"=>"Tipo de Compañia"),
    "descripcion"      =>array("header"=>"Descripción.",     "type"=>"textbox", "width"=>"210px", "req_type"=>"rt", "title"=>"Descripción" ),
    "nivel_autoridad"   =>array("header"=>"Nivel Autoridad",  "type"=>"textbox",  "width"=>"50px", "title"=>"Nivel Autoridad"),

    "flag_habilitado"   =>array("header"=>"Habilitado",       "type"=>"textbox", "width"=>"210px", "title"=>"Habilitado",
			"source"=>"self", "view_type"=>"radiobutton"	)
  );

//"source"=>"self"|$fill_from_array, "view_type"=>"dropdownlist(default)|radiobutton", "radiobuttons_alignment"=>"horizontal|vertical", "multiple"=>"false", "multiple_size"=>"4"),

  $dgrid->setColumnsInEditMode($em_columns);
##  *** set auto-genereted eName_1.FieldName > 'a' AND TableName_1.FieldName < 'c'"
##  *** Ex.: "on_js_event"=>"onclick='alert(\"Yes!!!\");'"
 $foreign_keys = array(
    "id_tipo_cia"=>array("table"=>"t_tipo_cia", "field_key"=>"id", "field_name"=>"descripcion", "view_type"=>"dropdownlist", "order_by_field"=>"descripcion", "order_type"=>"ASC")
 ); 
 $dgrid->setForeignKeysEdit($foreign_keys);

//  $dgrid->setAutoColumnsInEditMode(true);
  
## +---------------------------------------------------------------------------+
## | 8. Bind the DataGrid:                                                     | 
## +---------------------------------------------------------------------------+
##  *** set debug mode & messaging options
	fMostrarEncabezadoPagina($i18n->getKey("menu.tipousuario_ver"));         
    $dgrid->bind();        
   fMostrarPiePagina();

    ob_end_flush();

################################################################################   


